import { nextTick } from '../util/next-tick.js'

let queue = []
let has = {}

export function queueWatcher(watcher) {
  debugger
  const id = watcher.id
  if (has[id] === undefined) {
    has[id] = true

    queue.push(watcher)

    // 异步调用
    nextTick(flushSchedulerQueue)
  }
}

function flushSchedulerQueue() {
  for (let i = 0; i < queue.length; i++) {
    const ele = queue[i]
    ele.run() // 执行 watcher 真正的更新操作
  }

  // 清空
  queue = []
  has = {}
}
